const {
  getSpecialColumnInfo,
  getUserLevel,
  addLookHistory,
  getSpecialCourse
} = require('../../../../api/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    specialData: [],
    specialCourse: [],
    active: 0,
    imgHost: '',
    buyPrice: 0,
    vipLevel: null,
    detailTitle: {
      0: '专栏介绍',
      1: '课程目录'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      imgHost: app.img_host,
    })
    /**
     * 获取专栏详情数据
     */
    //获取到feature列表中传递过来的id
    const FEATURE_ID = app.id
    //1为专栏 2为课程
    const FEATURE = 1
    console.log(FEATURE_ID)
    getSpecialColumnInfo(FEATURE_ID).then(special => {
      this.setData({
        buyPrice: special.data.price
      })
      if (special.data.look_auth == 1) {
        this.setData({
          buyPrice: 0
        })
      }
      this.getLevel(special)
      this.setData({
        specialData: special.data
      })
    }).catch(err => {
      console.log(err)
    })
    /**
     * 获取vip等级
     */

    /**
     * 添加一条观看记录
     */
    addLookHistory(FEATURE_ID, FEATURE).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  },
  //获取的用户的vip等级并处理返回
  getLevel(special) {
    getUserLevel().then(res => {
      //获取到用户的vip等级
      const user_level = res.level
      //如果为1的话就是vip
      if (user_level == 1) {
        this.setData({
          //计算出vip的优惠额度
          buyPrice: this.data.buyPrice * special.data.vip_discount
        })
        //如果为2的话就是svip
      } else if (user_level == 2) {
        this.setData({
          //计算出svip的优惠额度
          buyPrice: this.data.buyPrice * special.data.svip_discount
        })
      }
      this.setData({
        vipLevel: res.level
      })
    }).catch(err => {
      console.log(err)
    })
  },
  /**
   * 列表切换到不同的课程中
   */
  change(e) {
    const index = e.detail.index + 1
    if (index === 2) {
      //获取到feature列表中传递过来的id
      const FEATURE_ID = app.id
      getSpecialCourse(FEATURE_ID).then(res => {
        console.log("index===2并且切换了对应的数据")
        this.setData({
          specialCourse: res.data
        })
      }).catch(err => {
        console.log(err)
      })
      this.setData({
        active: this.data.active + 1
      })
    } else if (index === 1) {
      this.setData({
        active: this.data.active - 1
      })
    }
  }
})